package com.docker.apps.afterservice.ui;

import android.arch.lifecycle.ViewModelProviders;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.dcbfhd.utilcode.utils.ToastUtils;
import com.docker.apps.R;
import com.docker.apps.databinding.ProRefundOrderActivityBinding;
import com.docker.apps.afterservice.vm.AfterServiceViewModel;
import com.docker.apps.order.vo.OrderVoV2;
import com.docker.common.common.router.AppRouter;
import com.docker.common.common.ui.base.NitCommonActivity;
import com.docker.common.common.utils.cache.CacheUtils;
import com.docker.common.common.utils.rxbus.RxBus;
import com.docker.common.common.utils.rxbus.RxEvent;
import com.docker.common.common.vo.UserInfoVo;
import com.docker.common.common.vo.entity.ReasonEntity;
import com.docker.common.common.widget.XPopup.BottomPopup;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.interfaces.XPopupCallback;

import java.util.HashMap;
import java.util.List;

/**
 * kxf -> 2020-02-18
 **/
@Route(path = AppRouter.ORDER_REFUND)
public class OrderRefundActivity extends NitCommonActivity<AfterServiceViewModel, ProRefundOrderActivityBinding> {
    String orderId, reasonId;

    @Override
    public void initView() {
        mToolbar.setTitle("申请退款");

        OrderVoV2 orderVoV2 = (OrderVoV2) getIntent().getSerializableExtra("order");
        orderId = orderVoV2.id;
        mBinding.refundT1.setOnClickListener(v -> {

            basePopupView.show();
        });
        mBinding.refundT2.setText(orderVoV2.receiveName);
        mBinding.refundT3.setText(orderVoV2.receiveTel);
        mBinding.refundT4.setOnClickListener(v -> {

        });
        reasonEntityList = CacheUtils.getReason();

        initPop();
        mBinding.refundT4.setOnClickListener(v -> {
            if (TextUtils.isEmpty(reasonId)) {
                ToastUtils.showShort("请选择退款原因");
                return;
            }
            HashMap<String, String> params = new HashMap<>();
            UserInfoVo userInfoVo = CacheUtils.getUser();
            params.put("memberid", userInfoVo.uid);
            params.put("uuid", userInfoVo.uuid);
            params.put("orderid", orderId);
            params.put("reason_id", reasonId);
            mViewModel.refundOrder(params);
        });


    }

    @Override
    public void initObserver() {

        mViewModel.mServerLiveData.observe(this, o -> {
            RxBus.getDefault().post(new RxEvent<>("refresh_buy", ""));

        });
    }

    @Override
    public void initRouter() {
        ARouter.getInstance().inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.pro_refund_order_activity;
    }

    @Override
    public AfterServiceViewModel getmViewModel() {
        return ViewModelProviders.of(this, factory).get(AfterServiceViewModel.class);
    }


    List<ReasonEntity> reasonEntityList;

    BasePopupView
            basePopupView;

    private void initPop() {
        BottomPopup bottomPopup = new BottomPopup(this, "reason_style");

        basePopupView = new XPopup.Builder(this).setPopupCallback(new XPopupCallback() {
            @Override
            public void onCreated() {
                ImageView iv_close = bottomPopup.findViewById(com.docker.common.R.id.iv_close);
                iv_close.setOnClickListener(v -> {
                    basePopupView.dismiss();

                });
                RadioGroup radioGroup = bottomPopup.findViewById(R.id.reason_r1);
                reasonEntityList.stream().forEach(e -> {
                    RadioButton radioButton = new RadioButton(radioGroup.getContext());
                    radioButton.setText(e.getContent());
                    radioButton.setCompoundDrawablePadding(10);
                    radioButton.setPadding(6, 6, 6, 6);
                    radioButton.setPaddingRelative(10, 10, 10, 10);
                    radioButton.setButtonDrawable(R.drawable.checkstate);
                    radioButton.setTextSize(12);
                    radioButton.setTextColor(Color.parseColor("#ff222222"));
                    RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    layoutParams.setMargins(8, 8, 8, 8);
                    radioGroup.addView(radioButton, layoutParams);

                });


                bottomPopup.findViewById(R.id.reason_b1).setOnClickListener(v -> {
                    bottomPopup.dismiss();
//                    ToastUtils.showLong(radioGroup.getCheckedRadioButtonId()-1 + "  ");
                    int index=(radioGroup.getCheckedRadioButtonId()) - 1;
                    ReasonEntity reason = reasonEntityList.get(index);
                    mBinding.refundT1.setText(reason.getContent());
                    reasonId = reason.getId();
//
                });

            }

            @Override
            public void beforeShow() {

            }

            @Override
            public void onShow() {

            }

            @Override
            public void onDismiss() {

            }

            @Override
            public boolean onBackPressed() {
                return false;
            }
        }).asCustom(bottomPopup);
        //.show();

    }

}